package com.happy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.happy.exception.ParamsIllegalException;
import com.happy.mapper.CardMapper;
import com.happy.mapper.CardReservationMapper;
import com.happy.mapper.CardTimeMapper;
import com.happy.model.dos.CardListDO;
import com.happy.model.dto.CardAddDTO;
import com.happy.model.dto.CardPageQuery;
import com.happy.model.dto.CardUpdateDTO;
import com.happy.model.entity.Card;
import com.happy.model.entity.CardReservation;
import com.happy.model.entity.CardTime;
import com.happy.model.result.PageResult;
import com.happy.model.vo.CardListByIdVO;
import com.happy.model.vo.CardListVO;
import com.happy.service.CardService;
import com.happy.utils.ValidationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class CardServiceImpl implements CardService {

    @Autowired
    private CardMapper cardMapper;
    @Autowired
    private CardReservationMapper cardReservationMapper;
    @Autowired
    private CardTimeMapper cardTimeMapper;

    private final LocalDateTime FOREVER_TIME = LocalDateTime.of(2099, 12, 31, 23, 59, 59);


    @Override
    public PageResult<CardListVO> getPage(CardPageQuery query) {
        query.checkPageParam();
        PageHelper.startPage(query.getPage(), query.getPageSize());
        List<CardListDO> list = cardMapper.pageList(query);
        // 类型转换 CardListDO -> CardListVO
        ArrayList<CardListVO> voList = new ArrayList<>();
        CardListVO vo = new CardListVO();
        for (CardListDO listDO : list) {
            LocalDateTime beginTime = listDO.getValidityBeginTime();
            LocalDateTime endTime = listDO.getValidityEndTime();
            String validity = beginTime + " 至 " + endTime;
            listDO.setValidity(validity);
            BeanUtil.copyProperties(listDO, vo);
            voList.add(vo);
        }
        PageInfo<CardListVO> pageResult = new PageInfo<>(voList);
        return PageResult.of(pageResult.getTotal(), pageResult.getList());
    }

    @Transactional
    @Override
    public void addCard(CardAddDTO cardAddDTO) {
        // 校验空值
        ValidationUtils.validate(cardAddDTO);
        if (cardAddDTO.getValidityType() == 1) {
            cardAddDTO.setValidityEndTime(FOREVER_TIME);
        }
        // @TODO 自动填充时间
        // LocalDateTime now = LocalDateTime.now();
        // cardAddDTO.setCardCreateTime(now);
        // cardAddDTO.setCardUpdateTime(now);
        cardMapper.insertCard(cardAddDTO);
        if (cardAddDTO.getReservation() == 2) {
            CardReservation cardReservations = cardAddDTO.getCardReservations();
            cardReservations.setCardId(cardAddDTO.getId());
            cardReservationMapper.insertCardReservations(cardReservations);
        }
        // 添加时间段表
        if (cardAddDTO.getUseTimeType() == 2) {
            CardTime useTimes = cardAddDTO.getUseTimes();
            useTimes.setCardId(cardAddDTO.getId());
            cardMapper.insertCardTime(useTimes);
        }
/*

        {
            "title": "好吃的水果蛋糕",
                "couponType": 1,
                "img": "http://yizhicai.mozisun.cn/share/tlias-share/20250518170311-wallhaven-yx2eqx.jpg",
                "draPrice": 188,
                "marPrice": 248,
                "limited": 10,
                "newCustomer": 2,
                "reservation": 2,
                "cardReservations": {
            "id": 0,
                    "cardId": 1,
                    "holiday": 10,
                    "workday": 10
        },
            "visibleState": 2,
                "validityType": 1,
                "validityBeginTime": "",
                "validityEndTime": "",
                "useTimeType": 2,
                "useTimes": {
            "id": 0,
                    "cardId": 1,
                    "monday": "duiduidui",
                    "tuesday": "buduibuduibudui",
                    "wednesday": "shidshidhisd",
                    "thursday": "nishiduide",
                    "friday": "",
                    "saturday": "",
                    "sunday": ""
        },
            "releasesNum": 10,
                "useRule": "规则就是没有规则"
        }
*/

    }

    @Transactional
    @Override
    public void deleteCard(Integer id) {
        if (id == null) {
            throw new ParamsIllegalException("参数非法");
        }
        Card card = cardMapper.selectById(id);
        if (card.getReservation() == 2) {
            cardReservationMapper.deleteCardReservations(id);
        }
        if (card.getUseTimeType() == 2) {
            cardTimeMapper.deleteCardTime(id);
        }
        cardMapper.deleteCard(id);
    }

    @Override
    public CardListByIdVO getCardListById(Integer id) {
        if (id == null) {
            throw new ParamsIllegalException("参数非法");
        }
        CardListByIdVO vo = cardMapper.getCardListById(id);
        // 有效类型1、永久2、部分天数
        if (vo.getValidityEndTime().equals(FOREVER_TIME)) {
            vo.setValidityType(1);
        } else {
            vo.setValidityType(2);
        }
        if (vo.getReservation() == 2){
            CardReservation cardReservations = cardReservationMapper.getReservation(id);
            vo.setCardReservations(cardReservations);
        }
        if (vo.getUseTimeType() == 2) {
            CardTime useTimes = cardTimeMapper.getCardTime(id);
            vo.setUseTimes(useTimes);
        }
        return vo;
    }

    @Transactional
    @Override
    public void updateCard(CardUpdateDTO cardUpdateDTO) {
        // 校验空值
        ValidationUtils.validate(cardUpdateDTO);
        if (cardUpdateDTO.getValidityType() == 1) {
            cardUpdateDTO.setValidityEndTime(FOREVER_TIME);
        }
        cardMapper.updateCard(cardUpdateDTO);
        if (cardUpdateDTO.getReservation() == 2) {
            CardReservation cardReservations = cardUpdateDTO.getCardReservations();
            Integer id = cardUpdateDTO.getId();
            cardReservations.setCardId(id);
            cardReservationMapper.deleteCardReservations(id);
            cardReservationMapper.insertCardReservations(cardReservations);
        }
        // 添加时间段表
        if (cardUpdateDTO.getUseTimeType() == 2) {
            CardTime useTimes = cardUpdateDTO.getUseTimes();
            Integer id = cardUpdateDTO.getId();
            useTimes.setCardId(id);
            cardTimeMapper.deleteCardTime(id);
            cardMapper.insertCardTime(useTimes);
        }
    }
}
